//
//  UIButton+Block.m
//  AppForInterview
//
//  Created by fangyuan ding on 2020/12/10.
//

#import "UIButton+Block.h"
#import <objc/message.h>
@implementation UIButton (Block)
static char overviewKey;

static char buttonType;

@dynamic event;

@dynamic  Type;

- (void)setType:(NSString *)Type{
    objc_setAssociatedObject(self, &buttonType, Type, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (NSString *)Type{
    NSString *Type = objc_getAssociatedObject(self, &buttonType);
    return Type;
}
- (void)handleControlEvent:(UIControlEvents)controlEvent withBlock:(ActionBlock)action{
    if(action){
        objc_removeAssociatedObjects(self);//移除这个对象button的所有关联
        objc_setAssociatedObject(self, &overviewKey,action,OBJC_ASSOCIATION_COPY_NONATOMIC);//设置
        [self addTarget:self action:@selector(callActionBlock:) forControlEvents:controlEvent];
    }
}
- (void)callActionBlock:(id)sender{// 获取关联对象
    ActionBlock block = (ActionBlock)objc_getAssociatedObject(self, &overviewKey);
    if(block){
        block();
    }
    
}
@end
